package org.simplecrm.service.user;

import org.simplecrm.domain.user.User;
import org.simplecrm.domain.usergroup.UserGroup;
import org.simplecrm.repository.user.UserDAO;
import org.simplecrm.repository.usergroup.UserGroupDAO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by oler on 12.09.14.
 */
@Service
public class UserServiceImpl implements UserService {

    @org.simplecrm.misc.loginject.Logger
    private Logger LOGGER;

    private static final String SUPPORT_GROUP_NAME = "Support";

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserGroupDAO userGroupDAO;

    @Override
    @Transactional
    public User getUserByName(String username) {

        User res;
        res = userDAO.getUserByName(username);
        return res;
    }

    @Override
    @Transactional
    public List<User> getAllSupports() {

        List<User> supportList;

        UserGroup supportGroup = userGroupDAO.getUsergroupByName(SUPPORT_GROUP_NAME);
        if (supportGroup == null) {
            LOGGER.error("There is no user group with name " + SUPPORT_GROUP_NAME);
            return new LinkedList<>();
        }
        supportList = userDAO.getUsersByGroupId(supportGroup.getId());
        return supportList;
    }
}
